package showtime.framework.engine
{
	/**
	 * Scene2D.as
	 * Yin Hao
	 * @version 1.0.0
	 * 
	 * Description.
	 * 
	 * Copyright (c) 2012 Yin Hao
	 * 
	 * Permission is hereby granted, free of charge, to any person obtaining a copy
	 * of this software and associated documentation files (the "Software"), to deal
	 * in the Software without restriction, including without limitation the rights
	 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	 * copies of the Software, and to permit persons to whom the Software is
	 * furnished to do so, subject to the following conditions:
	 * 
	 * The above copyright notice and this permission notice shall be included in
	 * all copies or substantial portions of the Software.
	 * 
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	 * THE SOFTWARE.
	 */
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import showtime.framework.events.AnimationMouseEvent;

	
	public class Scene2D extends Sprite implements IEntity
	{
		private var lastOperateObject:Entity2D = null;
		
		private var defaultGroup:Group2D = null;
	
		public var active:Boolean = false;
		
		public function Scene2D()
		{
			super();
			active = true;
			defaultGroup = new Group2D();
		}
		
		public function create():void
		{
			graphics.beginFill(0x000000, 0);			
			graphics.drawRect(0, 0, World2D.width, World2D.height);			
			graphics.endFill();
			
			addEventListener(MouseEvent.CLICK, clickHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

		}
				
		public function addEntity(entity:*):IEntity
		{
			addChild(entity);
			return defaultGroup.addEntity(entity);
		}
		
		
		public function removeEntity(entity:*):IEntity
		{
			removeChild(entity);
			return defaultGroup.removeEntity(entity);
		}
		
		public function update():void
		{
			active && defaultGroup.update();			
		}
		
		
		public function render():void
		{	
			visible && defaultGroup.render();
		}
		
		public function destroy():void
		{
			defaultGroup.destroy();
			
			active = false;
			visible = false;
			
			parent != null && parent.removeChild(this);
			
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		
			removeEventListener(MouseEvent.CLICK, clickHandler);
			removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);		
			
			
		}
		
		private function mouseMoveHandler(evt:MouseEvent):void
		{
			evt.stopImmediatePropagation();
			
			var objects:Array = stage.getObjectsUnderPoint(localToGlobal(new Point(mouseX, mouseY)));			
			
			var object:* = null;
			
			var isExist:Boolean = false;
			
			while (objects.length != 0)
			{
				object = objects.pop();
				
				if (object == this)
				{
					if (lastOperateObject != null && !isExist)
					{
						dispatchEvent(new AnimationMouseEvent(AnimationMouseEvent.ROLL_OUT, lastOperateObject));
						
						lastOperateObject = null;
					}
					break;
				}
				
				if (object is Entity2D)
				{				
					if (BitmapData(object.bitmapData).getPixel32(object.mouseX, object.mouseY))
					{
						isExist = true;
						
						if (object != lastOperateObject)
						{
							if (lastOperateObject != null)
							{					
								dispatchEvent(new AnimationMouseEvent(AnimationMouseEvent.ROLL_OUT, lastOperateObject));
								
							}
							
							lastOperateObject = object;
					
							dispatchEvent(new AnimationMouseEvent(AnimationMouseEvent.ROLL_OVER, object));
							break;
						}
						
					}
				}
			}
			
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			var objects:Array = stage.getObjectsUnderPoint(localToGlobal(new Point(mouseX, mouseY)));			
			
			while (objects.length != 0)
			{
				var object:* = objects.pop();
				
				if (object == this)
				{
					return;
				}
				
				if (object is Entity2D)
				{
					evt.stopImmediatePropagation();
							
					if (BitmapData(object.bitmapData).getPixel32(object.mouseX, object.mouseY))
					{
						dispatchEvent(new AnimationMouseEvent(AnimationMouseEvent.CLICK, object));
						break;
					}

					
				}
			}
		}
		
	}
}